from os import listdir
from os.path import splitext
import requests
from re import compile
from time import strftime, localtime


def union_qa(all_questions_, all_answers_, subject=1):
    max_ = len(all_answers_)
    # print('所有问题长度', len(all_questions_))
    # print('所有答案长度', len(all_answers_))
    count = 0
    try:
        for key in list(all_questions_.keys()):
            if count <= max_:
                all_questions_[key] = all_answers_[count]
                count += 1
        return all_questions_
    except IndexError:
        return all_questions_


def get_all_html(path_dir_):
    """
    接受文件夹路径，返回所有html文件路径的列表
    :param path_dir_:
    :return:
    """
    filenames_lst = listdir(path_dir_)
    html_lst_ = []
    for item in filenames_lst:
        if splitext(item)[1] == '.html':
            html_lst_.append('D:\\send_to_song\\{}'.format(str(item)))
    return html_lst_


def get_all_xlsx(path_dir_):
    """
    接受文件夹路径，返回所有xlsx文件路径的列表
    :param path_dir_:
    :return:
    """
    filenames_lst = listdir(path_dir_)
    xlsx_lst_ = []
    for item in filenames_lst:
        if splitext(item)[1] == '.xlsx':
            xlsx_lst_.append('D:\\send_to_song\\{}'.format(str(item)))
    return xlsx_lst_


def get_passwd_gonggao_version():
    passwd_ = ''
    try:
        url = 'http://192.167.192.167:8848/exe_config/config.html'
        str_html = requests.get(url)
        str_html.encoding = 'utf-8'
        html_text_ = str_html.text
        gonggao = ''
        version = 0
        passwd_obj = compile(r'<em.*?>(?P<passwd>.*?)</em>')
        passwd_ret = passwd_obj.finditer(html_text_)
        for i in passwd_ret:
            passwd_ = i.group('passwd')
        gonggao_obj = compile(r'<p.*?>(?P<gonggao>.*?)</p>')
        gonggao_ret = gonggao_obj.finditer(html_text_)
        for i in gonggao_ret:
            gonggao = i.group('gonggao')
        version_obj = compile(r'<i.*?>(?P<version>.*?)</i>')
        version_ret = version_obj.finditer(html_text_)
        for i in version_ret:
            version = i.group('version')
        return True, str(passwd_), str(gonggao), int(version)
    except requests.exceptions.ConnectionError:
        # 返回值依次为，密码，是否联网获取成功，公告，版本号
        return hash('bugsong'), False, '', 0


def download():
    try:
        url = 'http://192.167.192.167:8848/exe_config/main.exe'
        main_bin = requests.get(url)
        f = open('D:\\main.exe', 'wb')
        f.write(main_bin.content)
        f.close()
        return True
    except requests.exceptions.ConnectionError:
        # 返回值依次为，密码，是否联网获取成功，公告，版本号
        return False


if __name__ == '__main__':
    path_dir = 'D:\\send_to_song\\'
    xlsx_lst = get_all_html(path_dir)
    print(xlsx_lst)
    # print(get_passwd_gonggao_version())
    # download()
    super_key_date = strftime("%Y%m%d", localtime())
    super_key_hour = strftime("%H", localtime())
    super_key = int(super_key_date) + int(super_key_hour) * 2
    print(int(super_key))
